
const { UI_Layer } = require("../../config/ui_def");
const { Sdk_Platform, WX_AD_CONFIG } = require("../../frame/constants");
cc.Class({
    extends: cc.Component,

    properties: {
        content: cc.Node,
        scroll: cc.ScrollView,
        close_btn: cc.Button,
        bottom: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},
    initPage() {
        if (!he.pool.getPool('adIcon')) {
            he.pool.createPoolSync('adIcon', 10, cc.Prefab, 'uimod/adIcon')
        }
        if (!he.pool.getPool('adIconSec')) {
            he.pool.createPoolSync('adIconSec', 5, cc.Prefab, 'uimod/adIconSec')
        }
    },

    updatePage(param) {
        this.isShowAd = false
        this.scroll.scrollToTop()
        this.adSwitch(true)
        this.close_btn.interactable = true
        let ver_info = he.local.getJson('ver_info')
        if (ver_info.banner_mistake) {
            this.close_btn.node.y = -550
        } else {
            this.bottom.name = WX_AD_CONFIG.bannerId[0]
            he.ad.changeBanner(true, this.bottom)
            this.close_btn.node.y = -330
            this.close_btn.interactable = true
        }
    },

    closeUI() {
        for (let i = (this.content.children.length); i > 0; i--) {
            this.content.children[i - 1].getComponent('adIcon').clear()
            he.pool.putElement('adIcon', this.content.children[i - 1])
        }
        this.node.targetOff(this)
        he.ad.changeBanner(false, this.bottom)
    },

    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    if (!common.showAd) return
                    he.ad.getTdAdJson(WX_AD_CONFIG.tmIcon[0], this.topSet.bind(this))
                    break
            }
        } else {
            //  关掉广告
        }
    },

    bottomSet(config) {
        let pre = he.pool.getElement('scrollAd')
        pre.parent = this.bottomAd
        pre.getComponent('scrollAd').init(config.creatives)
    },

    topSet(config) {
        if (config.isOpen) {
            for (let i = 0; i < config.creatives.length; i++) {
                let pre = he.pool.getElement('adIcon')
                pre.position = cc.Vec2.ZERO
                pre.parent = this.content
                pre.opacity = 255
                let spt = pre.getComponent('adIcon')
                spt.init(config.creatives[i], true, 0, 1)
            }
        }
    },

    closeServer() {
        if (gg.uiManager.preScene == 'Main') {
            this.uiBase.closePage()
        } else {
            this.uiBase.closePage()
            gg.uiManager.addPage(UI_Layer.Cover, {
                name: 'allAtPlayAdPage',
                cover: true,
            })
        }

    },

    rewardwxServer() {
        if (!this.isShowAd) {
            this.isShowAd = true
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    this.close_btn.interactable = false
                    let ver_info = he.local.getJson('ver_info')
                    if (ver_info.banner_mistake) {
                        this.scheduleOnce(() => {
                            this.bottom.name = WX_AD_CONFIG.bannerId[0]
                            he.ad.changeBanner(true, this.bottom)
                        }, 1.5)
                        this.scheduleOnce(() => {
                            this.close_btn.node.y = -330
                            // he.ad.changeBanner(false, this.bottom)
                            this.close_btn.interactable = true
                        }, 3)
                    } else {
                        this.closeServer()
                    }
                    break
                default:
                    this.closeServer()
                    break
            }
        } else {
            this.closeServer()
        }
    },

});
